$buff_min = 2400; #40 minutes
$buff_max = 4800; #80 minutes
$countdown = 5; # Countdown time to MGB in minutes, default 5 minutes
$announce_min = 1200; #20 minutes
$announce_max = 2400; #40 minutes

@SpellList = ("DoNotUse","Wall of Slaugther","Arcstone","Barindu","Blightfire Moors","Bloodfields","Buried Sea","Butcher","Cobalt Scar","Commons","Dawnshroud","Feerrott","Great Divide","Grimling","Iceclad","Karana","Lavastorm","Misty","Natimbi","South Ro","Steamfont","Stonebrunt","Surefall Glade","Combines","Nexus","The Steppes","Toxxulia","Twilight","Undershore","Wakening Lands","Emerald Jungle","Sky Fire Mountains","Spirit of Wolf", "Spirit of Eagle", "Skin like Nature", "Protection of the Cabbage", "Shield of Thorns");
@SpellCost = ("Crashs",  "50",               "50",      "30",     "50",              "50",         "50",        "5",      "15",         "5",      "20",        "5",       "15",          "20",      "15",     "5",     "5",        "5",    "30",     "5",       "5",        "5",         "5",             "10",      "20",   "50",         "5",       "20",      "50",        "15",            "10",            "10",                "2",              "10",              "10",               "30",                        "10");
@SpellID =   ("The",     "6179",             "8965",    "5731",   "9957",            "6184",       "11981",     "553",    "1440",       "551",    "2429",      "556",     "1438",        "2419",    "1434",   "550",   "554",      "558",  "4966",   "555",     "557",      "3792",      "2020",          "1517",    "2432", "9954",       "552",     "2424",    "8235",      "1398",          "1737",          "1736",              "278",            "2517",            "423",              "2188",                      "356");
@SpellType = ("Zone!",   "Port",             "NoPort",  "Port",   "NoPort",          "Port",       "NoPort",    "Port",   "Port",       "Port",   "Port",      "Port",    "Port",        "Port",    "Port",   "Port",  "Port",     "Port", "Port",   "Port",    "Port",     "Port",      "Port",          "Port",    "Port", "NoPort",     "Port",    "Port",    "NoPort",    "Port",          "Port",          "Port",              "Buff",           "Buff",            "Buff",             "Buff",                      "Buff");

sub EVENT_SPAWN {
   MASS_GROUP_BUFF();
   ANNOUNCE();
}

sub ANNOUNCE {
   $announce_timer = int(rand($announce_max)) + $announce_min;
   quest::settimer("announce_gawen", $announce_timer); # Random announcements
}

sub MASS_GROUP_BUFF {
   $mgb_timer = int(rand($buff_max)) + $buff_min;
   quest::settimer("mgb_gawen", $mgb_timer);
   $minutes = $countdown;
}

sub EVENT_TIMER {
   if($timer eq "mgb_gawen") {
      quest::shout("Mass group buff at tree by Guild Hall, come one, come all!!");
      quest::stoptimer("mgb_gawen");
      quest::stoptimer("announce_gawen");
      quest::settimer("countdown_gawen", 60); # 60 second count before each countdown announcement
   }
 
   if($timer eq "countdown_gawen") {
      if($minutes > 0) {
         quest::shout("Casting Protection of the Cabbage MGB in " . $minutes . " minutes.");
         $minutes--;
      } else {
         quest::shout("Casting Protection of the Cabbage now !");
         quest::stoptimer("countdown_gawen");
         $npc->SetAppearance(0);
         my @moblist = $entity_list->GetMobList();
         foreach $ent (@moblist) {
            my $dist = $ent->CalculateDistance($npc->GetSpawnPointX(),$npc->GetSpawnPointY(),$npc->GetSpawnPointZ());
            plugin::DoAnim(cast);
            if(int($dist)<100 && $ent->IsClient()) {
               $ent->SpellFinished(2188, $ent, 10);
            }
         }
         ANNOUNCE();
         MASS_GROUP_BUFF();
      }
   }

   if($timer eq "announce_gawen") {
      my $random_number = int(rand(5));
      if ($random_number == 0) {
         quest::shout("Seeking passage to faraway Lands? See me by the tree next to the guild lobby.");
      } elsif ($random_number == 1) {
         quest::shout("My druidic powers will aid you in Battle!");
        } elsif ($random_number == 2) {
         quest::shout("If i can't Port you, no one can. Seek me out by the tree next to the guild lobby.");
      } elsif ($random_number == 3) {
         quest::shout("With my Spirit of the Eagle you can even outrun a Cheetah");
      } elsif ($random_number == 4) {
         quest::shout("Forcing donations for druid buffs and ports by the tree next to the guild lobby.");
        }
      $npc->SetAppearance(int(rand(2)));
      ANNOUNCE();
   } 
}

sub EVENT_SAY {

   if (!$cpl) { # We create the List of Spells just once per zone load > npc hail.
      $cpl = 1;
      my $n = 0;
      @buffs = ();
      while ($SpellList[$n]) {
         if ($SpellType[$n] eq "Port") {
            my $SpellList = quest::saylink($n, 1, $SpellList[$n]);
            if ($SpellCost[$n] == 5) {
               $port_classic = $port_classic."$SpellList, ";
            } elsif ($SpellCost[$n] == 10) {
               $port_kunark = $port_kunark."$SpellList, ";
            } elsif ($SpellCost[$n] == 15) {
               $port_velious = $port_velious."$SpellList, ";
            } elsif ($SpellCost[$n] == 20) {
               $port_luclin = $port_luclin."$SpellList, ";
            } elsif ($SpellCost[$n] == 30)  {
               $port_gates = $port_gates."$SpellList, ";
            } elsif ($SpellCost[$n] == 50) {
               $port_omens = $port_omens."$SpellList, ";
            }
         } elsif ($SpellType[$n] eq "Buff") {
            my $SpellList = quest::saylink($n, 1, $SpellList[$n]);
            my $SpellCost = $SpellCost[$n];
            push (@buffs, "$SpellList for $SpellCost PP");
         }
      $n++;
      }
      $buff = join(", ",@buffs);
   }
   
   my $NPCName = $npc->GetCleanName();
   $client->Message(315, " ");
   if ($text=~/hail/i) {
      my $sl1 = quest::saylink("blessings", 1);
      my $sl2 = quest::saylink("portal", 1);
      $client->Message(315, "$NPCName says, 'Hail, $name. Have you come for my $sl1 or a $sl2 to somewhere?'");
      plugin::DoAnim(bowto);
   } elsif ($text=~/blessings/i) {
      my $n = 30;
      $client->Message(315, "$NPCName says, 'Please, tell me what blessing you wish to recieve $name.'");
      $client->Message(315, "$buff");
   } elsif ($text=~/portal/i) {
      $client->Message(315, "$NPCName says, 'Where do you wish to go $name?'");
      $client->Message(315, "Classic : $port_classic for 5pp.");
      $client->Message(315, "Kunark : $port_kunark for 10pp.");
      $client->Message(315, "Velious : $port_velious for 15pp.");
      $client->Message(315, "Luclin : $port_luclin for 20pp.");
      $client->Message(315, "Gates of Discord : $port_gates for 30pp.");
      $client->Message(315, "Omens of War : $port_omens for 50pp.");
   } else {
      if (($text =~ m/\d/) && $SpellList[$text]) {
         my $PCMoney = $client->GetCarriedMoney();
         my $Cost = $SpellCost[$text]*1000;
         if ($PCMoney >= $Cost) {
            $client->Message(315, "$NPCName says, 'Thank you $name! Good luck out there!");
            plugin::DoAnim(cast);
            quest::selfcast($SpellID[$text]);
            $client->TakeMoneyFromPP($Cost, true);
         } else {
            $client->Message(315, "$NPCName says, 'I am sorry $name, you don't have enough Money with you.");
         }
      }
   }
}

sub EVENT_ITEM {
   my $NPCName = $npc->GetCleanName();
   $client->Message(315, "$NPCName says, 'Thank you, but you should hold on to it.'");
   plugin::return_items(\%itemcount);
   if ($platinum != 0 || $gold !=0 || $silver != 0 || $copper != 0) {
      quest::givecash($copper, $silver, $gold, $platinum);
   }
}